package com.hue.gulimail.gulimailware.listeren;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.alibaba.fastjson.TypeReference;
import com.hue.common.to.mq.WareOrderTaskDetailTo;
import com.hue.common.utils.R;
import com.hue.gulimail.gulimailware.entity.WareOrderTaskEntity;
import com.hue.gulimail.gulimailware.feign.OrderFeignService;
import com.hue.gulimail.gulimailware.service.WareOrderTaskService;
import com.hue.gulimail.gulimailware.service.WareSkuService;
import com.hue.gulimail.gulimailware.vo.OrderVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

import java.io.IOException;
import java.util.List;

/**
 * @Author: 姚凤桥
 * @Date: 2021/12/30 23:23
 * @Description: 监听mq的stock-release-stock-queue队列
 */
@Component
public class WareListener {
    @Autowired
    private WareOrderTaskService wareOrderTaskService;
    @Autowired
    private OrderFeignService orderFeignService;
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 需要解锁库存的情况：
     * 1.订单支付成功
     * 2.订单超时被系统自动取消
     * 3.被用户手动取消
     * 4.订单生成成功/库存锁定成功，但是在接下来的过程中出现了异常导致订单滚，此时也要解锁库存
     *
     * @param message
     * @param channel
     * @param wareOrderTaskDetailToList
     */
    @RabbitListener(queues = "stock.release.stock.queue")
    public void stockReleaseStockQueueListener(Message message, Channel channel,
                                               List<WareOrderTaskDetailTo> wareOrderTaskDetailToList) throws IOException {
        if (wareOrderTaskDetailToList == null && wareOrderTaskDetailToList.size() == 0) {
            return;
        }

        //查询出订单号
        QueryWrapper<WareOrderTaskEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", wareOrderTaskDetailToList.get(0).getTaskId());
        WareOrderTaskEntity orderTask = wareOrderTaskService.getOne(wrapper);
        String orderSn = orderTask.getOrderSn();

        //查询订单信息
        try {
            R r = orderFeignService.getBySn(orderSn);
            if ((Integer) r.get("code") == 0) {
                OrderVo order = r.getData(new TypeReference<OrderVo>() {
                });

                //订单被回滚/已经取消/待付款需要解锁库存
                if (order == null || order.getStatus() == 0 || order.getStatus() == 4) {
                    wareSkuService.unlockStock(wareOrderTaskDetailToList);
                }
                //签收消息
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                //查询订单信息出现异常,拒收这条消息，并将消息重新放回队列
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //拒收消息
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
